package com.hongsheng.util;

import com.google.gson.Gson;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class NewGetDistance {

//    private static final String key = "5e18913c6af32ec37edec0f854196c9e";
        private static final String key = "5b85fb266fad1b2ea02f24903ebefacf";


    public static void main(String[] args){

        //222222根据地址查询距离的
        String start = "湖南长沙天心区";
        String end = "湖南株洲天元区";
//        String startLonLat = getLonLat(start);//获取到开始地址的经纬度
//        String endLonLat = getLonLat(end);//获取到达地址的经纬度
        Double distan = distance(start,end);
        System.out.println(distan);
    }

    /**
     * 高德地图WebAPI : 行驶距离测量
     */
    public static Double distance(String origins,String destination) {
        String startLonLat = getLonLat(origins);//获取到开始地址的经纬度
        String endLonLat = getLonLat(destination);//获取到达地址的经纬度
        int type = 1;//type=1，即驾车导航距离
        String url = "http://restapi.amap.com/v3/distance?"
                + "origins="+startLonLat
                +"&destination="+endLonLat
                +"&type="+type
                +"&key="+ key;
        //这里用的Gson，也可以用下面的JSON，都试了一下
        Gson gson = new Gson();
        Map map = new Gson().fromJson(getLoadJson(url),Map.class);
        List list=(List)map.get("results");
        Map<String,String> map1 = (Map) list.get(0);
        String distance = map1.get("distance");
        Double aDouble = Double.valueOf(distance);
        Double v = aDouble / 1000.0;
        System.out.println(v);
        return v;
    }

    /**
     * Java http 请求  获取结果
     */
    public static String getLoadJson (String url) {
        StringBuilder json = new StringBuilder();
        try {
            //下面那条URL请求返回结果无中文，可不转换编码格式
            URL urlObject = new URL(url);
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    /**
     * 根据给的地址获得经纬度信息
     * @param address
     * @return
     */
    private static String getLonLat(String address){
        //返回输入地址address的经纬度信息, 格式是 经度,纬度
        String queryUrl = "http://restapi.amap.com/v3/geocode/geo?key="+key+"&address="+address;
        String queryResult = getLoadJson(queryUrl);  //高德接口返回的是JSON格式的字符串
        
		//这里也可以用Gson接
		//JSONObject是Map,可以通过key访问值
		//JSONArray是List,可以通过索引访问值
        JSONObject jo = new JSONObject().fromObject(queryResult);
        JSONArray ja = jo.getJSONArray("geocodes");
        return new JSONObject().fromObject(ja.getString(0)).get("location").toString();
    }
}
